<meta charset="utf-8">
(#) Add Explicit Cast

!!! WARNING: Add Explicit Cast
   This is a warning.

Id
:   `FindViewByIdCast`
Summary
:   Add Explicit Cast
Severity
:   Warning
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   3.0.0 (October 2017)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ViewTypeDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ViewTypeDetectorTest.kt)

In Android O, the `findViewById` signature switched to using generics,
which means that most of the time you can leave out explicit casts and
just assign the result of the `findViewById` call to variables of
specific view classes.

However, due to language changes between Java 7 and 8, this change may
cause code to not compile without explicit casts. This lint check looks
for these scenarios and suggests casts to be added now such that the
code will continue to compile if the language level is updated to 1.8.

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/main/java/test/pkg/ImplicitCastTest2.java:9:Warning: Add explicit
cast here; won't compile with Java language level 1.8 without it
[FindViewByIdCast]
    checkNotNull1(findViewById(R.id.textView)).setAlpha(0.5f); // WARN
                  ---------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/main/java/test/pkg/ImplicitCastTest2.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import android.view.View;

import test.pkg.R;

public class ImplicitCastTest2 extends MyActivityStub {
    public void test() {
        checkNotNull1(findViewById(R.id.textView)).setAlpha(0.5f); // WARN
        checkNotNull2(findViewById(R.id.textView)).setAlpha(0.5f); // OK 1
        checkNotNull3(findViewById(R.id.textView)).setAlpha(0.5f); // OK 2
        checkNotNull1(findViewById(R.id.textView)); // OK 3
        checkNotNull2(findViewById(R.id.textView)); // OK 4
        checkNotNull3(findViewById(R.id.textView)); // OK 5
        checkNotNull1((View)findViewById(R.id.textView)); // OK 6
        checkNotNull2((View)findViewById(R.id.textView)); // OK 7
        checkNotNull3((View)findViewById(R.id.textView)); // OK 8
        View view1 = checkNotNull1(findViewById(R.id.textView)); // OK 9
        View view2 = checkNotNull2(findViewById(R.id.textView)); // OK 10
        View view3 = checkNotNull3(findViewById(R.id.textView)); // OK 11
        findViewById(R.id.textView); // OK 12
    }

    public static <T> T checkNotNull1(T reference) {
        if(reference == null) {
            throw new NullPointerException();
        } else {
            return reference;
        }
    }

    public static <T extends View> T checkNotNull2(T reference) {
        if(reference == null) {
            throw new NullPointerException();
        } else {
            return reference;
        }
    }

    public static View checkNotNull3(View reference) {
        if(reference == null) {
            throw new NullPointerException();
        } else {
            return reference;
        }
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`build.gradle`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~groovy linenumbers
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ViewTypeDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `ViewTypeDetector.testCastNeeded`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("FindViewByIdCast")
  fun method() {
     findViewById(...)
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("FindViewByIdCast")
  void method() {
     findViewById(...);
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection FindViewByIdCast
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="FindViewByIdCast" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'FindViewByIdCast'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore FindViewByIdCast ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>